package com.zrrd.io.objectStream;

import com.zrrd.io.pojo.Student;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

/**
 * @author WanJl
 * @version 1.0
 * @title ObjectInputStreamDemo01
 * @description 对象反序列化流
 * @create 2023/10/15 9:41
 */
public class ObjectInputStreamDemo01<O> {

    public O readObject(String fileName)throws Exception{
        //创建流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
        //读取对象
        Object obj = ois.readObject();
        //强制类型转换
        O o=(O)obj;
        //释放资源
        ois.close();
        return o;
    }
    public static void main(String[] args) throws Exception {
//        ObjectInputStreamDemo01<User> oisd = new ObjectInputStreamDemo01();
//        User o = oisd.readObject("D:/zs");
//        //父类不具备子类特有的方法，所以需要把父类转换成子类，再调用子类特有的方法，
//        //o.getPassword();
//        System.out.println(o.toString());

        ObjectInputStreamDemo01<Student> oisd = new ObjectInputStreamDemo01();
        Student student = oisd.readObject("D:/zs");
        System.out.println(student);
    }
}
